﻿//-----------------------------------------------------------------------
// <copyright file="EnumerableX.cs" company="Bugger Sisters Inc.">
//     Copyright (c) Bugger Sisters Inc. All rights reserved.
// </copyright>
// <author>Sergey Tyshkovets</author>
//-----------------------------------------------------------------------

namespace MagicTheGathering.Framework.Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public static class EnumerableX
    {
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection)
        {
            return collection == null || !collection.Any();
        }

        public static String ToSeparatedString<T>(this IEnumerable<T> source, String separator)
        {
            return String.Join(separator, source.Select(e => e.ToString()).ToArray());
        }

        public static String ToSeparatedString<T>(this IEnumerable<T> source, String separator, String format)
        {
            return String.Join(separator, source.Select(e => String.Format(format, e)).ToArray());
        }

        public static IEnumerable<T> Distinct<T, TKey>(this IEnumerable<T> source, Func<T, TKey> expression)
        {
            if (source != null)
            {
                return source.GroupBy(expression).Select(i => i.First());
            }
            return Enumerable.Empty<T>();
        }

        public static IEnumerable<T> RemoveAll<T>(this IEnumerable<T> source, Predicate<T> predicate)
        {
            if (source == null)
            {
                return Enumerable.Empty<T>();
            }

            List<T> list = source.ToList<T>();
            list.RemoveAll(predicate);
            return list;
        }
    }
}
